package com.rickpan.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * AI生成报告实体类
 * 用于存储和管理AI生成的工作报告
 */
@Entity
@Table(name = "generated_reports")
@EntityListeners(AuditingEntityListener.class)
public class GeneratedReport {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "user_id", nullable = false)
    private Long userId;

    @Column(nullable = false, length = 200)
    private String title;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private ReportType type;

    @Column(name = "period_start", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate periodStart;

    @Column(name = "period_end", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate periodEnd;

    @Column(name = "original_content", nullable = false, columnDefinition = "TEXT")
    private String originalContent;

    @Column(name = "edited_content", columnDefinition = "TEXT")
    private String editedContent;

    @Column(nullable = false)
    private Integer version = 1;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private ReportStatus status = ReportStatus.DRAFT;

    @Column(columnDefinition = "JSON")
    private String tags;

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    // 报告类型枚举
    public enum ReportType {
        DAILY("日报"),
        WEEKLY("周报"),
        MONTHLY("月报");

        private final String displayName;

        ReportType(String displayName) {
            this.displayName = displayName;
        }

        public String getDisplayName() {
            return displayName;
        }
    }

    // 报告状态枚举
    public enum ReportStatus {
        DRAFT("草稿"),
        FINAL("最终版"),
        ARCHIVED("已归档");

        private final String displayName;

        ReportStatus(String displayName) {
            this.displayName = displayName;
        }

        public String getDisplayName() {
            return displayName;
        }
    }

    // 构造函数
    public GeneratedReport() {}

    public GeneratedReport(Long userId, String title, ReportType type, 
                          LocalDate periodStart, LocalDate periodEnd, String originalContent) {
        this.userId = userId;
        this.title = title;
        this.type = type;
        this.periodStart = periodStart;
        this.periodEnd = periodEnd;
        this.originalContent = originalContent;
    }

    // Getter和Setter方法
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public ReportType getType() { return type; }
    public void setType(ReportType type) { this.type = type; }

    public LocalDate getPeriodStart() { return periodStart; }
    public void setPeriodStart(LocalDate periodStart) { this.periodStart = periodStart; }

    public LocalDate getPeriodEnd() { return periodEnd; }
    public void setPeriodEnd(LocalDate periodEnd) { this.periodEnd = periodEnd; }

    public String getOriginalContent() { return originalContent; }
    public void setOriginalContent(String originalContent) { this.originalContent = originalContent; }

    public String getEditedContent() { return editedContent; }
    public void setEditedContent(String editedContent) { this.editedContent = editedContent; }

    public Integer getVersion() { return version; }
    public void setVersion(Integer version) { this.version = version; }

    public ReportStatus getStatus() { return status; }
    public void setStatus(ReportStatus status) { this.status = status; }

    public String getTags() { return tags; }
    public void setTags(String tags) { this.tags = tags; }

    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }

    public LocalDateTime getUpdatedAt() { return updatedAt; }
    public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }

    /**
     * 获取当前使用的内容（优先使用编辑后内容）
     */
    public String getCurrentContent() {
        return editedContent != null && !editedContent.trim().isEmpty() 
               ? editedContent : originalContent;
    }

    /**
     * 检查是否已被编辑
     */
    public boolean isEdited() {
        return editedContent != null && !editedContent.trim().isEmpty();
    }

    /**
     * 生成报告标题
     */
    public static String generateTitle(ReportType type, LocalDate periodStart, LocalDate periodEnd) {
        switch (type) {
            case DAILY:
                return String.format("%s 工作日报", periodStart.toString());
            case WEEKLY:
                return String.format("%s 至 %s 工作周报", periodStart.toString(), periodEnd.toString());
            case MONTHLY:
                return String.format("%d年%d月 工作月报", periodStart.getYear(), periodStart.getMonthValue());
            default:
                return "工作报告";
        }
    }
}
